<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title></title>

	<script type="text/javascript" src="opencv_haarcascade_frontalface_alt.js"></script>
	<script type="text/javascript" src="opencv_haarcascade_eye.js"></script>
	<script type="text/javascript" src="opencv_haarcascade_upper_body.js"></script>
	<script type="text/javascript" src="opencv_haarcascade_mouth.js"></script>
</head>
<body>
	<script>
		// [
		// 	[
		// 		-1, // index
		// 		0.8226894140243530, // stage threshold
		//		// tree
		// 		[
		//			// node 1
		// 			[
		// 				3, 7, 14, 4, -1, // rect1
		// 				3, 9, 14, 2, 2, // rect 2
		// 				4.0141958743333817e-003, // node threshold
		// 				0.0337941907346249, // left
		// 				0.8378106951713562 // right
		// 			],
		//			// node 2
		// 			[
		// 				3, 7, 14, 4, -1,
		// 				3, 9, 14, 2, 2,
		// 				4.0141958743333817e-003,
		// 				0.0337941907346249,
		// 				0.8378106951713562
		// 			]
		// 		]
		// 	]
		// ];

		var toFloat = function(v) {
			// return parseFloat(parseFloat(v).toFixed(1));
			return parseFloat(v);
		},

		toInt = function(v) {
			return parseInt(v, 10);
		},

		convert = function(haarcascade) {
			var stages = [],
				hstages = haarcascade.stages,
				i,
				j;

			for (i = 0; i < hstages.length; i++) {
				var stage = [],
					trees = [],
					hstage = hstages[i],
					htrees = hstage.trees,
					parent = toInt(hstage.parent),
					stageThreshold = toFloat(hstage.stage_threshold);

				for (j = 0; j < htrees.length; j++) {
					var node = [],
						hnode = htrees[j][0],
						hnodeThreshold = toFloat(hnode.threshold),
						hnodeLeft = toFloat(hnode.left_val),
						hnodeRight = toFloat(hnode.right_val),
						hnodeRects = hnode.feature.rects,
						hr,
						r;

						for (r = 0; r < hnodeRects.length; r++) {
							hr = hnodeRects[r].split(" ").map(toFloat),
							node = node.concat(hr);
						}

						node.push(hnodeThreshold, hnodeLeft, hnodeRight);
						trees.push(node);
				}

				stage.push(parent, stageThreshold, trees);
				stages.push(stage);
			}

			console.log(stages);

			return JSON.stringify(stages);
		};

		// output

		// var json = convert(opencv_haarcascade_frontalface_alt);
		// var json = convert(opencv_haarcascade_eye);
		// var json = convert(opencv_haarcascade_upper_body);
		var json = convert(opencv_haarcascade_mouth);

		console.log(json);
	</script>
</body>
</html>